<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('人员头像')" prop="logoInfo">
            <UploadAvatar :logo-info="form.logoInfo" @appendData="uploadImg($event)" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('人员名称')" prop="staffName">
            <el-input v-model="form.staffName" :placeholder="$t('人员名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('类型')">
            <el-select v-model="form.staffType" clearable value="" :placeholder="$t('类型')">
              <el-option
                v-for="dict in staffTypeOption"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('职位')" prop="postType">
            <el-input v-model="form.postType" :placeholder="$t('职位')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('联系电话')" prop="phone">
            <el-input v-model="form.phone" :placeholder="$t('联系电话')" show-word-limit maxlength="20" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('邮箱地址')" prop="email">
            <el-input v-model="form.email" :placeholder="$t('邮箱地址')" show-word-limit maxlength="200" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div v-show="!btnHide" slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="company:staff" />
    </div>
  </div>
</template>

<script>
import { saveCompanyStaff, getCompanyStaff, uploadAvatar } from '@/api/company/staff/companyStaff'
import UploadAvatar from '@/components/Avatar/uploadAvatar'
export default {
  components: {
    UploadAvatar
  },
  data() {
      return {
          // 表单参数
          form: {
            logoInfo: undefined
          },
          // 遮罩层
          loading: false,
          // 表单校验
          rules: {
            staffName: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            staffType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
            phone: [
              {
                pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                message: this.$t('请输入正确的手机号码'),
                trigger: 'blur'
              }
            ],
            email: [
              {
                type: 'email',
                message: this.$t('请输入正确的邮箱地址'),
                trigger: ['blur', 'change']
              }
            ]
          },
          // 类型下拉
          staffTypeOption: [],
          // 是否隐藏保存等按钮
          btnHide: false
      }
  },
  mounted() {
    this.$bus.$emit('async_done')
  },
  created() {
    this.getDicts('company_person').then(response => {
      this.staffTypeOption = response.data
    })
  },
  methods: {
      // 初始化数据
      init(data) {
          this.loading = true
          if (data != null && data.id !== undefined) {
              getCompanyStaff(data.id).then(response => {
                  this.form = response.data
                  this.loading = false
              })
          } else {
              this.reset()
              this.loading = false
          }
          this.form.companyId = this.$route.query.companyId
      },
      // 表单重置
      reset() {
          this.form = {
          id: undefined,
          companyId: undefined,
          tycId: undefined,
          staffName: undefined,
          logoInfo: undefined,
          staffType: '2',
          postType: undefined,
          phone: undefined,
          email: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
          remark: undefined,
          delFlag: undefined
          }
          this.resetForm('form')
      },
      // 取消按钮
      cancel() {
          this.$parent.$parent.formOpen = false
          this.$parent.$parent.reset()
      },
      // 关闭form页面
      closeForm() {
          // 关闭form页面遮罩层
          this.loading = false
          // 关闭form页面
          this.$parent.$parent.formOpen = false
          // 刷新list页面
          this.$parent.$parent.getList()
      },
      /** 提交按钮 */
      submitForm: function() {
          this.$refs['form'].validate(valid => {
              if (valid) {
                  this.loading = true
                  if (this.form.source === '2') {
                    this.form.source = '3'
                  }
                  saveCompanyStaff(this.form).then(response => {
                      if (response.code === 200) {
                          this.msgSuccess(this.$t('保存成功'))
                          if (this.$route.query.companyId) {
                            this.$store.dispatch('bussRisk/UPDATE_BUSS_RISK_COUNT', { id: this.$route.query.companyId })
                          }
                          this.closeForm()
                      } else {
                          this.msgError(response.msg)
                      }
                      this.loading = false
                  }).catch(() => {
                    this.loading = false
                  })
              } else {
                this.locationError(document)
              }
          })
      },
      // 上传图片
      uploadImg(data) {
        const formData = new FormData()
        formData.append('avatarfile', data)
        formData.append('id', this.form.id)
        uploadAvatar(formData).then(response => {
          if (response.code === 200) {
            this.form.logoInfo = response.msg
            this.msgSuccess(this.$t('修改成功'))
          } else {
            this.msgError(response.msg)
          }
        })
      }
  }

}
</script>
